package com.itheima.appserver.controller;

import com.itheima.appserver.interceptor.UserHandler;
import com.itheima.appserver.service.UserInfoService;
import com.itheima.appserver.service.UserService;
import com.itheima.model.domain.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 保存用户信息
     * 请求头中携带token
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo,
                                       @RequestHeader("Authorization") String token) {
        //设置userinfo的id

        userInfo.setId(UserHandler.getId());
        //保存用户
        userInfoService.save(userInfo);
        return ResponseEntity.ok(null);
    }

    @PostMapping("/loginReginfo/head")
    public ResponseEntity uploadloginReginfo(@RequestBody MultipartFile headPhoto,
                                             @RequestHeader("Authorization") String token) throws IOException {
        //设置userinfo的id
        //调用service
        userInfoService.update(headPhoto, UserHandler.getId());
        return ResponseEntity.ok(null);
    }
}
